package ru.amse.stroganova.test.commands;

import java.awt.Point;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import ru.amse.stroganova.graph.AbstractGraph;
import ru.amse.stroganova.graph.DirectedGraph;
import ru.amse.stroganova.graph.Edge;
import ru.amse.stroganova.graph.Vertex;
import ru.amse.stroganova.presentation.EdgePresentation;
import ru.amse.stroganova.presentation.GraphPresentation;
import ru.amse.stroganova.ui.command.Command;
import ru.amse.stroganova.ui.command.CommandFactory;
import ru.amse.stroganova.ui.visual.GraphElementSelection;

/**
 * Provides tests for change edge weight command.
 * 
 * @author Dasha Stroganova
 * 
 */
public class WeightChangeCommand {
	private AbstractGraph graph;

	private GraphPresentation graphPresentation;
	
	private GraphElementSelection graphElementSelection;

	private CommandFactory commandFactory;

	private Command weightChangeCommand;

	private int oldWeight;

	private int newWeight;

	private Edge edge;

	/**
	 * Creates test graph, presentation and command.
	 * 
	 */
	@Before
	public void setUp() {
		graph = new DirectedGraph();
		Vertex vertex1 = graph.addVertex();
		Vertex vertex2 = graph.addVertex();
		oldWeight = 2;
		newWeight = 10;
		edge = graph.connect(vertex1, vertex2, oldWeight);
		graphPresentation = new GraphPresentation(graph, true, true);

		graphElementSelection = new GraphElementSelection(graphPresentation);
		commandFactory = new CommandFactory(graphPresentation, graphElementSelection);
		
		EdgePresentation ep = selectWeight();
		
		weightChangeCommand = commandFactory.getWeightChangeCommand(oldWeight,
				newWeight, ep);
	}

	/**
	 * Tests undo method.
	 */
	@Test
	public void testUndo() {
		weightChangeCommand.execute();
		weightChangeCommand.undo();
		Assert.assertTrue(edge.getWeight() == oldWeight);
	}

	/**
	 * Tests execute method.
	 */
	@Test
	public void testExecute() {
		weightChangeCommand.execute();
		Assert.assertTrue(edge.getWeight() == newWeight);
	}

	private EdgePresentation selectWeight() {
		for (int i = 0; i < 500; i++) {
			for (int j = 0; j < 500; j++) {
				if (graphElementSelection.getEdgeWithSelectedWeight(new Point(i, j)) != null) {
					return graphElementSelection.getEdgeWithSelectedWeight(new Point(i, j));
				}
			}
		}
		return null;
	}
}
